using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Threading;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Forms;
using System.Windows.Markup;
using System.Windows.Xps.Packaging;
using Common;
using Microsoft.Win32;
using Report;

namespace MainView.Windows
{
	public partial class ExperimentPetientReportManageWindow : Window, IComponentConnector
	{
		private ObservableCollection<TemplateFile> _Files = new ObservableCollection<TemplateFile>();

		private BackgroundWorker worker = new BackgroundWorker();

		private string File_Docx_Ext = "docx";

		public ExperimentPetientReportManageWindow()
		{
			InitializeComponent();
			worker.WorkerSupportsCancellation = true;
			worker.WorkerReportsProgress = true;
			worker.DoWork += worker_DoWork;
			worker.ProgressChanged += worker_ProgressChanged;
			string[] files = Directory.GetFiles(Utility.GetReportTemplatePath(), "*.docx");
			string[] array = files;
			foreach (string text in array)
			{
				try
				{
					if ((File.GetAttributes(text) & FileAttributes.Hidden) == FileAttributes.Hidden)
					{
						continue;
					}
				}
				catch (Exception)
				{
					continue;
				}
				List<IDocumentPaginatorSource> documents = new List<IDocumentPaginatorSource> { null };
				_Files.Add(new TemplateFile
				{
					FileName = text.Replace(Utility.GetReportTemplatePath(), ""),
					Path = text,
					Documents = documents
				});
			}
			listBoxTemplateFile.ItemsSource = _Files;
			listBoxTemplateFile.SelectedIndex = 0;
		}

		private void RefreshDocument()
		{
			RefreshDocument(listBoxTemplateFile.SelectedIndex);
			if (listBoxTemplateFile.SelectedItem != null)
			{
				TemplateFile templateFile = listBoxTemplateFile.SelectedItem as TemplateFile;
				if (templateFile != null && templateFile.Documents.Count > 0)
				{
					documentViewer.Document = templateFile.Documents[0];
				}
			}
			else
			{
				documentViewer.Document = null;
			}
		}

		private void worker_DoWork(object sender, DoWorkEventArgs e)
		{
			Thread.Sleep(1000);
			BackgroundWorker backgroundWorker = sender as BackgroundWorker;
			for (int i = 0; i < _Files.Count; i++)
			{
				if (backgroundWorker.CancellationPending)
				{
					break;
				}
				if (_Files[i].Documents[0] == null)
				{
					Utility.DoEvents();
					backgroundWorker.ReportProgress(i);
				}
			}
		}

		private void worker_ProgressChanged(object sender, ProgressChangedEventArgs e)
		{
			RefreshDocument(e.ProgressPercentage);
		}

		private void listBoxTemplateFile_SelectionChanged(object sender, SelectionChangedEventArgs e)
		{
			RefreshDocument();
		}

		private void RefreshDocument(int idx)
		{
			if (idx >= 0 && idx < _Files.Count && _Files[idx].Documents[0] == null)
			{
				string systemTempFile = Utility.GetSystemTempFile();
				WordOperator.ExportToXPS(_Files[idx].Path, systemTempFile);
				if (!string.IsNullOrEmpty(systemTempFile) && File.Exists(systemTempFile))
				{
					XpsDocument xpsDocument = new XpsDocument(systemTempFile, FileAccess.Read);
					IDocumentPaginatorSource fixedDocumentSequence = xpsDocument.GetFixedDocumentSequence();
					xpsDocument.Close();
					_Files[idx].Documents[0] = fixedDocumentSequence;
				}
			}
		}

		private void btnExport_Click(object sender, RoutedEventArgs e)
		{
			TemplateFile templateFile = listBoxTemplateFile.SelectedItem as TemplateFile;
			if (templateFile == null)
			{
				return;
			}
			System.Windows.Forms.SaveFileDialog saveFileDialog = new System.Windows.Forms.SaveFileDialog();
			saveFileDialog.Filter = string.Format("{0}{1}|*.{2}", "", Utility.GetMessage("File"), File_Docx_Ext);
			saveFileDialog.FileName = Path.GetFileName(templateFile.Path);
			saveFileDialog.FilterIndex = 1;
			saveFileDialog.InitialDirectory = Utility.GetMyDocumentFolder();
			if (saveFileDialog.ShowDialog() != System.Windows.Forms.DialogResult.OK)
			{
				return;
			}
			bool flag = true;
			while (Utility.IsFileInUse(saveFileDialog.FileName))
			{
				if (System.Windows.MessageBox.Show(string.Format(Utility.GetMessage("msg_file_in_use"), Path.GetFileName(saveFileDialog.FileName)), Utility.GetMessage("Warning"), MessageBoxButton.YesNo, MessageBoxImage.Question, MessageBoxResult.No) == MessageBoxResult.No)
				{
					flag = false;
					break;
				}
			}
			if (flag)
			{
				File.Copy(templateFile.Path, saveFileDialog.FileName, true);
				System.Windows.MessageBox.Show(Utility.GetMessage("msg_patient_report_export_sussess"));
			}
		}

		private void btnDelete_Click(object sender, RoutedEventArgs e)
		{
			TemplateFile templateFile = listBoxTemplateFile.SelectedItem as TemplateFile;
			if (templateFile != null && System.Windows.MessageBox.Show(Utility.GetMessage("msg_delete_patient_report_file_question"), Utility.GetMessage("Warning"), MessageBoxButton.YesNo, MessageBoxImage.Question, MessageBoxResult.No) == MessageBoxResult.Yes)
			{
				File.Delete(templateFile.Path);
				int num = listBoxTemplateFile.SelectedIndex;
				_Files.Remove(templateFile);
				if (num >= _Files.Count)
				{
					num = _Files.Count - 1;
				}
				listBoxTemplateFile.SelectedIndex = num;
			}
		}

		private void btnImport_Click(object sender, RoutedEventArgs e)
		{
			TemplateFile templateFile = null;
			Microsoft.Win32.OpenFileDialog openFileDialog = new Microsoft.Win32.OpenFileDialog();
			openFileDialog.Filter = string.Format("{0}{1}|*.{2}", "", Utility.GetMessage("File"), File_Docx_Ext);
			openFileDialog.FileName = string.Empty;
			openFileDialog.FilterIndex = 1;
			openFileDialog.DefaultExt = "docx";
			openFileDialog.Multiselect = true;
			if (openFileDialog.ShowDialog() != true)
			{
				return;
			}
			string[] fileNames = openFileDialog.FileNames;
			foreach (string text in fileNames)
			{
				templateFile = new TemplateFile();
				string name = text.Substring(text.LastIndexOf("\\") + 1);
				templateFile.Path = Utility.GetReportTemplatePath() + name;
				templateFile.FileName = name;
				if (_Files.Where((TemplateFile s) => s.FileName == name).Count() > 0)
				{
					if (System.Windows.MessageBox.Show(Utility.GetMessage("msg_file_exist_cover"), Utility.GetMessage("Warning"), MessageBoxButton.YesNo, MessageBoxImage.Question, MessageBoxResult.No) == MessageBoxResult.No)
					{
						continue;
					}
					bool flag = true;
					while (Utility.IsFileInUse(templateFile.Path))
					{
						if (System.Windows.MessageBox.Show(string.Format(Utility.GetMessage("msg_file_in_use"), Path.GetFileName(templateFile.Path)), Utility.GetMessage("Warning"), MessageBoxButton.YesNo, MessageBoxImage.Question, MessageBoxResult.No) == MessageBoxResult.No)
						{
							flag = false;
							break;
						}
					}
					if (flag)
					{
						File.Copy(text, templateFile.Path, true);
					}
					_Files.Where((TemplateFile s) => s.FileName == name).FirstOrDefault().Documents[0] = null;
					RefreshDocument(_Files.IndexOf(_Files.Where((TemplateFile s) => s.FileName == name).FirstOrDefault()));
					RefreshDocument();
					continue;
				}
				bool flag2 = true;
				while (Utility.IsFileInUse(templateFile.Path))
				{
					if (System.Windows.MessageBox.Show(string.Format(Utility.GetMessage("msg_file_in_use"), Path.GetFileName(templateFile.Path)), Utility.GetMessage("Warning"), MessageBoxButton.YesNo, MessageBoxImage.Question, MessageBoxResult.No) == MessageBoxResult.No)
					{
						flag2 = false;
						break;
					}
				}
				if (flag2)
				{
					File.Copy(text, templateFile.Path, true);
				}
				List<IDocumentPaginatorSource> list = new List<IDocumentPaginatorSource>();
				list.Add(null);
				templateFile.Documents = list;
				_Files.Add(templateFile);
			}
		}
	}
}
